﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildOptimizer
{
    class BoCommandQueenInject : BoCommand
    {
        private const int _time = 0;
        private const float _energy = 25.0f;
        private const commandID _commandID = commandID.queenInject;

        private int _hatchIndex = -1;
        public BoCommandQueenInject(int hatchIndex)
        {
            _hatchIndex = hatchIndex;
        }

        public override void execute(BoGoal goal, BoDecisionNode node)
        {
            bool found = false;
            int queenIndex = -1;
            for (int i = 0; i < node.gameState.nbQueens && !found; i++)
            {
                if (node.gameState.getQueen(i).energy >= _energy)
                {
                    queenIndex = i;
                    found = true;
                }
            }
            node.gameState.getQueen(queenIndex).removeEnergy(_energy);

            checkIfMoreRequired(goal, node);
            BoTask t = new BoTask(_time, delegate(BoDecisionNode n)
            {
                n.gameState.getHatchery(_hatchIndex).inject();
            });
            node.gameState.addTask(t);
        }

        public static bool checkIfMoreRequired(BoGoal goal, BoDecisionNode node)
        {
            return true;
        }
        public static bool isAvailable(BoGameState gs, int hatchIndex)
        {
            if (gs.getHatchery(hatchIndex).isInjected)
            {
                return false;
            }
            for (int i = 0; i < gs.nbQueens; i++)
            {
                if (gs.getQueen(i).energy >= _energy)
                {
                    return true;
                }
            }
            return false;
        }
        public static bool isAffordable(BoGameState gs)
        {
            return true;
        }

        public override int getCommandCode()
        {
            int code = ((int)_commandID * 100) + _hatchIndex;
            return code;
        }
        public override commandID getCommandID()
        {
            return _commandID;
        }
        public override string toString()
        {
            string str = "Queen -> inject hatchery " + _hatchIndex;
            return str;
        }
    }
}
